package com.example.jh352160.surfingtv.thematic_details.customView;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;

import com.example.jh352160.surfingtv.R;
import com.example.jh352160.surfingtv.network.item.AllCommodityItem;
import com.example.jh352160.surfingtv.network.retrofit.CommonRetrofit;
import com.example.jh352160.surfingtv.network.service.AllCommodityService;
import com.example.jh352160.surfingtv.thematic_details.customView.adpter.CommodityPagerAdapter;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

/**
 * Created by jh352160 on 2016/8/1
 */

public class AllCommodity extends AppCompatActivity {

    private static final int NETWORK_OK = 0;
    Context context;
    ViewPager commodityPager;
    TabLayout tabLayout;
    ImageView close_image, pre_image, next_image;
    int contentId;
    List<String> fromList, detilUrlList, titleList, imageUrlList, priceList;
    List<Boolean> isLikeList;
    List<Integer> targetIdList;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case NETWORK_OK:
                    init();

            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.thematic_all_commodity);

        close_image = (ImageView) findViewById(R.id.close_image);
        pre_image = (ImageView) findViewById(R.id.pre_button);
        next_image = (ImageView) findViewById(R.id.next_button);
        commodityPager = (ViewPager) findViewById(R.id.commodityPager2);
        tabLayout = (TabLayout) findViewById(R.id.tabLayout);
        contentId = getIntent().getIntExtra("contentId", 0);

        //api
        Retrofit retrofit = CommonRetrofit.getRetrofit();
        AllCommodityService commodityService = retrofit.create(AllCommodityService.class);
        Call<com.example.jh352160.surfingtv.network.item.AllCommodityItem> call = commodityService.getItem(contentId);
        call.enqueue(new Callback<com.example.jh352160.surfingtv.network.item.AllCommodityItem>() {
            @Override
            public void onResponse(Call<com.example.jh352160.surfingtv.network.item.AllCommodityItem> call, Response<com.example.jh352160.surfingtv.network.item.AllCommodityItem> response) {
                List<com.example.jh352160.surfingtv.network.item.AllCommodityItem.DataBean> dataBeen = response.body().getData();
                fromList = new ArrayList<>();
                detilUrlList = new ArrayList<>();
                imageUrlList = new ArrayList<>();
                priceList = new ArrayList<>();
                isLikeList = new ArrayList<>();
                targetIdList = new ArrayList<>();
                titleList = new ArrayList<>();
                for (AllCommodityItem.DataBean i : dataBeen) {
                    fromList.add(i.getProductFrom());
                    detilUrlList.add(i.getDetailUrl());
                    imageUrlList.add(i.getImageUrl());
                    priceList.add("￥ " + i.getPrice());
                    isLikeList.add(i.getFavorite() == 1);
                    targetIdList.add(i.getProductId());
                    titleList.add(i.getName());
                }
                Message message = new Message();
                message.what = NETWORK_OK;
                handler.sendMessage(message);
            }

            @Override
            public void onFailure(Call<com.example.jh352160.surfingtv.network.item.AllCommodityItem> call, Throwable t) {

            }
        });
    }

    private void tabSelect() {
        for (int i = 1; i < imageUrlList.size(); i++) {
            AllcommodityTabItem imageView2 = new AllcommodityTabItem(this, imageUrlList.get(i));
            tabLayout.getTabAt(i).setCustomView(imageView2);
        }
        AllcommodityTabItem imageView = new AllcommodityTabItem(this, imageUrlList.get(0));
        imageView.setSelect(true);
        tabLayout.getTabAt(0).setCustomView(imageView);
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                View view = tab.getCustomView();
                ((AllcommodityTabItem) view).setSelect(true);
                tab.setCustomView(view);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                View view = tab.getCustomView();
                ((AllcommodityTabItem) view).setSelect(false);
                tab.setCustomView(view);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    private void init() {
//        List<String> names = new ArrayList<>();
//        List<String> prices = new ArrayList<>();
//        for (int i = 0; i < 5; i++) {
//            names.add("name" + i);
//            prices.add("" + (100 * i + 100));
//        }
        CommodityPagerAdapter adapter = new CommodityPagerAdapter(this, fromList, detilUrlList, titleList, imageUrlList, priceList, isLikeList, targetIdList);
        commodityPager.setAdapter(adapter);

        tabLayout.setupWithViewPager(commodityPager);
        tabSelect();
        tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);

        close_image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AllCommodity.this.finish();
            }
        });

        pre_image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                commodityPager.arrowScroll(View.FOCUS_LEFT);
            }
        });

        next_image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                commodityPager.arrowScroll(View.FOCUS_RIGHT);
            }
        });
    }
}
